package com.mybook.app.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by hdl on 2017/11/1.
 * <p>
 * ID 生成规则
 */
public final class IdUtils {

    //城市代码city(6位）+YYYYMMDDHHMISSsss（17位）+hostid（5位）+4位随机数

    public static String getId(String cityCode) {

        StringBuilder sb = new StringBuilder();
        sb.append(cityCode);
        sb.append(getDateFormat());
        sb.append("00001");//TODO 机器编码 暂时写死
        sb.append(randomNumber(4));
        return sb.toString();
    }

    private static String getDateFormat() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return simpleDateFormat.format(new Date());
    }

    //随机数字
    private static String randomNumber(Integer length) {
        StringBuffer sb = new StringBuffer();
        Random r = new Random(System.nanoTime());
        for (int i = 0; i < length; i++) {
            sb.append(r.nextInt(10) + "");
        }
        return sb.toString();
    }
}
